package com.ygqh.baby.controller.admin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ygqh.baby.ao.FileType;
import com.ygqh.baby.service.FileService;


@Controller
@RequestMapping(value="/admin/file")
public class FileOsController  {		
	
	@Value("${image.domain}")
	private String imageDomain;
	@Autowired
	private FileService fileService;
	
	
	
	@RequestMapping(value ="uploadFile")   
	@ResponseBody
	public Map<Object,Object> uploadFile(@RequestParam("uploadFile")   MultipartFile uploadFile, FileType fileType
			,  HttpServletRequest request, HttpServletResponse response) throws Exception {  		
		Map<Object,Object> result = new HashMap<Object, Object>();
		if(fileType==null){
			fileType = FileType.Temp;
		}
		
		if(!uploadFile.isEmpty()){
			if(fileType.getMaxSize()!=null&&fileType.getMaxSize()<uploadFile.getSize()) {
				result.put("message", "文件尺寸过大");
				return result;
			}
			String url = fileService.upload2Oss(fileType, uploadFile);
			result.put("fileUrl", url);
		}
		return result;
	}
	
	
	
	@RequestMapping(value = "uploadImage")
	@ResponseBody
	public Map<Object, Object> uploadImage(@RequestParam("uploadFile") MultipartFile uploadFile, @RequestParam(required = true) FileType[] fileType,Boolean isFill, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if(isFill == null)
			isFill = true;
		Map<Object, Object> result = new HashMap<Object, Object>();
		if (!uploadFile.isEmpty()) {
			String url = fileService.uploadImage(uploadFile, fileType, request,isFill);
			result.put("fileUrl", url);
			result.put("message", "");
		}
		return result;
	}
	
}